package com.example.a0512eventpress;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

public class PressEvent extends AppCompatActivity {
    CheckBox checkBox1;
    EditText entryText;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_press_event);
        checkBox1 = (CheckBox) findViewById(R.id.checkBox1);
        entryText = (EditText) findViewById(R.id.entryText);
        textView = (TextView) findViewById(R.id.txtView);

        entryText.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                int metaState = keyEvent.getMetaState();
                int unicodeChar = keyEvent.getUnicodeChar();
                String msg = "";
                msg += "按键动作:" + String.valueOf(keyEvent.getAction()) + "\n";
                msg += "按键代码:" + String.valueOf(keyCode) + "\n";
                msg += "按键字符:" + (char) unicodeChar + "\n";
                msg += "UNICODE:" + String.valueOf(unicodeChar) + "\n";
                msg += "事件重复次数:" + String.valueOf(keyEvent.getRepeatCount()) + "\n";
                msg += "功能键状态:" + String.valueOf(metaState) + "\n";
                msg += "硬件编码:" + String.valueOf(keyEvent.getScanCode()) + "\n";
                msg += "按键标志:" + String.valueOf(keyEvent.getFlags()) + "\n";
                textView.setText(msg);
                if (checkBox1.isChecked())
                    return true;
                else
                    return false;
            }
        });
    }
}
